﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Article_management_system
{
    class Sqldt
    {
        public static readonly string link = "server=.;database=Blogs;uid=sa;pwd=123456.";
        public static DataTable Query(string sql)//查询和初始化
        {
            SqlConnection connection = new SqlConnection(link);
            SqlDataAdapter adapter = new SqlDataAdapter(sql,connection);
            DataTable dt = new DataTable();
            connection.Open();
            adapter.Fill(dt);
            return dt;
        }

        public static int Modifydeleteadd(string sql)//修改、删除、添加
        {
            SqlConnection connection = new SqlConnection(link);
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);
            return command.ExecuteNonQuery();
        }

        public static SqlDataReader Judge(string sql)//登录、注册
        {
            SqlConnection connection = new SqlConnection(link);
            connection.Open();
            SqlCommand command = new SqlCommand(sql,connection);
            return command.ExecuteReader();
        }

        public static bool Repetition(string Name)//判断用户名是否重复使用
        {
            string sql = "select Username from Users";
            SqlDataReader dr = Sqldt.Judge(sql);//链接数据库
            bool pd = false;
            while (dr.Read())//循环数据库数据
            {
                if (Name.Equals(dr[0]))//判断账号是否重复
                {
                    pd = true;
                    break;
                }
            }
            return pd;
        }

        public static DataTable Refresh()//刷新
        {
            string sql = "SELECT Id '编号',Title '文章标题',Author '文章作者',CreatedTime '创建时间'  FROM Articles";
            DataTable dt=Query(sql);
            return dt;
        }

    }
}
